#ifndef __STD_H
#define __STD_H

#include "main.h"
#include "ads8684.h"
#include "pca6416.h"
#include "max31865.h"
#include "max31856.h"

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

#ifndef bool
#define bool uint8_t
#endif

#define M_SET_BIT(REG, BIT) ((REG) |= (1UL << BIT))
#define M_CLR_BIT(REG, BIT) ((REG) &= ~(1UL << BIT))
#define M_READ_BIT(REG, BIT) ((REG >> BIT) & (1UL))

#define GROOUP_MAX 4

#define JINGDU 10000u // 最大65535

/* 硬件芯片错误*/
enum eDevErrType
{
	e_err_io_out = 0,
	e_err_io_in,
	e_err_outputcurrent,
	e_err_dac,
	e_err_adc,
	e_err_max31865,
	e_err_max31856,
};

enum eControlMode
{
    // 手动开始
    // 手动结束
    // 自动开始
    e_ControlMode_ManualEnd = 0,
    e_ControlMode_ManualStart = 1,
    e_ControlMode_AutoStart = 2

};

/* 传感器类型选择 */
enum eSensorType
{
    // 从站获取PT100型
    // 从站获取K型热电偶
    e_SensorType_PT100_Slave = 0,
    e_SensorType_K_Slave = 1,
    // 主站输入PT100型
    // 主站输入K型热电偶
    e_SensorType_PT100_Master = 2,
    e_SensorType_K_Master = 3
};

enum eError
{
    e_err_ok,
    e_err_spibusy,
};

typedef struct
{
    uint8_t ch;   // 通道
    uint8_t en;   // 使能
    uint32_t cnt; // 计数值	//单位1ms
    uint32_t ccr; // 反转值	//单位1ms
    uint32_t ctl; // 周期		//单位1ms
    uint32_t tick;
    uint8_t state;

} t_soft_pwm, *p_soft_pwm;

typedef struct
{
    float P;
    float I;
    float D;
    float Ti;
    float Td;
    float Ctl; // 一个控制周期 单位s
    float PV;  // 测量值
    float SV;  /// 给定值
    float out;
    float cha;
    float cha_old;
    float cha_older;
    float num;
    uint32_t D_cnt;

} t_PID, *p_PID; //

typedef struct
{
    float tx_current_temperature;

    float rx_current_temperature;

    uint16_t control_mode;
    int32_t target_temperature;
    float P;
    float I;
    float D;
    uint8_t sensor_type;
    uint8_t max_power;

    float sensor_temperature;  // 传感器温度
    float current_temperature; // 当前温度
    uint16_t analog_in_value;  // adc值
    t_PID pid;
    t_soft_pwm soft_pwm;

    // 用于周期性轮询输入
    uint16_t tick;
    uint32_t temtime;

	uint32_t err;

} t_group;

typedef struct
{
    t_group group[GROOUP_MAX];
    uint8_t spi_busy;
} t_ethcat_dev, *p_ethcat_dev;

void ethercat_sync(p_ethcat_dev ptl);

#endif
